__author__ = 'shawhen'

from ..shared import minwindef as m_shared_minwindef

from .. import Kernel32


def CreateMutex(MutexAttributes, bInitialOwner, lpName):
    """
    :param MutexAttributes: 安全属性，None<->NULL
    :param lpName: None<->NULL
    :return mutex handle
    """
    if MutexAttributes is None:
        MutexAttributes = m_shared_minwindef.NULL
    if lpName is None:
        lpName = m_shared_minwindef.NULL
    return Kernel32.Kernel32_dll.CreateMutexW(MutexAttributes, bInitialOwner, lpName)


def WaitForSingleObject(hHandle, dwMillisecons):
    """
    :param dwMillisecons a python int
    :return a python int
    """
    dwMillisecons = m_shared_minwindef.DWORD(dwMillisecons)
    return Kernel32.Kernel32_dll.WaitForSingleObject(hHandle, dwMillisecons)


def ReleaseMutex(hMutex):
    """
    :return False/True
    """
    result = Kernel32.Kernel32_dll.ReleaseMutex(hMutex)
    if result == m_shared_minwindef.FALSE:
        result = False
    elif result == m_shared_minwindef.TRUE:
        result = True
    return result